<?php
/*
@author Aurelien Deharbe

This file is part of Cute-Client.

Cute-Client is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Cute-Client is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with Cute-Client.  If not, see <http://www.gnu.org/licenses/>.
*/

class CSSession {
  
  var $callbacks;
  var $port;
  var $server;
  var $socket;
  
  function __construct() {
    $this->port = 5001;
    $this->server = False;
    error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
    while ($this->server == False) {
      $this->server = socket_create_listen($this->port);
      $this->port ++;
    }
    error_reporting(E_ALL ^ E_NOTICE);
    $this->port--;
  }
  
  function qs($cmd) {
    $cmd = $cmd . "\n";
    socket_write($this->socket, $cmd, strlen($cmd));
    $ret = socket_read($this->socket, 1024);
    $result = json_decode($ret);
    return $result[count($result)-1];
  }

  function registerCallback($obj, $evt, $clb) {
    $this->registerTypedCallback($obj, $evt, $clb, "");
  }
  
  function registerDataCallback($data, $clb) {
    $this->registerTypedCallback($data, "qsdatachanged", $clb, "");
  }
  
  function registerTypedCallback($obj, $evt, $clb, $tps) {
    $cmd = "scripts.subscribe('" . $obj;
    $cmd .= "','".$evt."','".$tps."','#me#')\n";
    socket_write($this->socket, $cmd, strlen($cmd));
    $ret = socket_read($this->socket, 1024);
    $ret = json_decode($ret);
    foreach ($ret as $i) {
      $this->callbacks[$i.".".$evt] = $clb;
    }
  }
  
  function mainloop() {
    while (True) {
      $ret = socket_read($this->socket, 1024);
      if ($ret == "") {
	break;
      }
      $jarray = json_decode($ret);
      $obj = $jarray[0];
      $evt = $obj.".".$jarray[1];
      if (! array_key_exists($evt, $this->callbacks)) {
	continue;
      }
      unset($jarray[0]);
      unset($jarray[0]);
      if (count($jarray) > 0) {
	$this->callbacks[$evt]($obj, $jarray);
      } else {
	$this->callbacks[$evt]($obj);
      }
    }
  }
  
  function putData($name, $value) {
    $value = json_encode($value);
    $cmd = "json.loads('".$value."')";
    $cmd .= " -> " . substr($name, 1, strlen($name)-2) . "\n";
    socket_write($this->socket, $cmd, strlen($cmd));
    socket_read($this->socket, 1024);
  }
  
  function getData($name) {
    $cmd = $name . "\n";
    socket_write($this->socket, $cmd, strlen($cmd));
    $ret = socket_read($this->socket, 1024);
    $result = json_decode($ret);
    return $result[count($result)-1];
  }
  
  function getPort() {
    return $this->port;
  }
  
  function accept() {
    $this->socket = socket_accept($this->server);
    socket_close($this->server);
  }
}

?>